var wardHomeWardDesc = "";
$(function(){
	
	//滚动条默认最上面
	scrollTo(0,0);
	tabId=$("#tabContent > .tab-pane.active").attr("id");
	
	getRoleName();
	//获取角色信息
	function getRoleName(){
		$.post(contextPath+'/wardMonitor/getUserInfo.dhc').done(function(result){
			wardHomeWardCode = result.wardCode;
			wardHomeWardDesc = result.wardDesc;
			paramTabPage[tabId].depCode = result.depCode;
			drawWardTables(wardHomeWardCode,wardHomeWardDesc);//病区累计数据与绩效考核指标的比较情况
			getClinicData(wardHomeWardCode);//病区下主诊组指标累计值与标杆值比较图表
			getClinicData_perf(wardHomeWardCode);
	    });
	}

	//病区累计数据与绩效考核指标的比较情况表格方法
	function drawWardTables(wardCode,wardDesc){
		var mrLot = paramTabPage[tabId].mrLot;
		var year= mrLot.substring(0,mrLot.indexOf("Y"));
		var month = mrLot.substring(mrLot.indexOf("Y")+1, mrLot.indexOf("M"));
		$('#'+tabId+' .year').html(year);
		$('#'+tabId+' .month').html(month);
		$.post(contextPath+'/Monitor/monitor.dhc', 
				{'mrLot':paramTabPage[tabId].mrLot, 
				 'mrType':paramTabPage[tabId].mrType,
				 'compareType':"std",
				 'pType': paramTabPage[tabId].pType,
				 'parwardCode':wardCode,
				 'monitorType':"ward",
				 'quotaCode':"AvgDaysAvgFeeDrugRatio",
				 'pardepgroupCode':"",
				 'pargroupName': "",
				 'parmdcCode':"",
				 'paradrgCode':"",
				 'pardrgCode':""}).done(function(result){	
			var titleData = result.data[0];
			buildDepQuotaTargetTable('#'+tabId+' #depUnfinished-table', titleData);
	    });
	}

	//科室累计数据与绩效考核指标的比较情况表格
	function buildDepQuotaTargetTable(selector,titleData){
		if (titleData!=undefined) {
			var idT='id1'+new Date();
			var tableHtml = '<table id="'+idT+'" class="stripe cc table" style="width:100%;">'
			+'<thead>'
			+'<tr>'
			+'<td>指标项</td>'
			+'<td>平均住院日</td>'
			+'<td>次均费用</td>'
			+'<td>药占比(%)</td>'
			+'</tr></thead>'
			+'<tr>'
			+'<td>累计值</td>'
			+'<td>'+compareValue(titleData.AvgDays,titleData.AvgDayscc)+'</td>'
			+'<td>'+compareValue(titleData.AvgFee,titleData.AvgFeecc)+'</td>'
			+'<td>'+comparePercent(titleData.DrugRatio,titleData.DrugRatiocc)+'</td>'
			+'</tr>'
			+'<tr>'
			+'<td>标杆值</td>';
			if(titleData.AvgDayscc == undefined){
				tableHtml += '<td>_</td>';
			}else{
				tableHtml += '<td>'+titleData.AvgDayscc+'</td>';
			}
			if(titleData.AvgFeecc == undefined){
				tableHtml += '<td>_</td>';
			}else{
				tableHtml += '<td>'+titleData.AvgFeecc+'</td>';
			}
			if(titleData.DrugRatiocc == undefined){
				tableHtml += '<td>_</td>';
			}else{
				tableHtml += '<td>'+titleData.DrugRatiocc+'</td>';
			}
			tableHtml +='</tr></table>';
			$('#'+tabId+' #depUnfinished-table').html(tableHtml);
		}
	}

	//病区下主诊组指标累计值与标杆值比较图表
	function getClinicData(wardCode){
		$.post(contextPath+'/Monitor/monitor.dhc', 
			{'mrLot':paramTabPage[tabId].mrLot, 
			 'mrType':paramTabPage[tabId].mrType,
			 'compareType':"manage",
			 'pType': paramTabPage[tabId].pType,
			 'pardepCode':paramTabPage[tabId].depCode,
			 'parwardCode':wardCode,
			 'monitorType':"clinic",
			 'quotaCode':"AvgDaysAvgFeeDrugRatio",
			 'pardepgroupCode':"",
			 'pargroupName': "",
			 'parmdcCode':"",
			 'paradrgCode':"",
			 'pardrgCode':""}).done(function(result){
			getClinicCompareChart(result.data);//科室下主诊组指标累计值与标杆值比较图
		});
	}
	
	//科室下主诊组指标累计值与标杆值比较图表
	function getClinicData_perf(wardCode){
		$.post(contextPath+'/Monitor/monitor.dhc', 
			{'mrLot':paramTabPage[tabId].mrLot, 
			 'mrType':paramTabPage[tabId].mrType,
			 'compareType':"manage",
			 'pType': paramTabPage[tabId].pType,
			 'parwardCode':wardCode,
			 'monitorType':"clinic",
			 'quotaCode':"DepPerfQuota",
			 'pardepgroupCode':"",
			 'pargroupName': "",
			 'parmdcCode':"",
			 'paradrgCode':"",
			 'pardrgCode':""}).done(function(result){
			getClinicTable(result.data);//各主诊组DRGs相关考核指标累计值比较表
		});
	}

	//科室下主诊组指标累计值与标杆值比较图
	function getClinicCompareChart(charts){
		var category = new Array();
		var dataAvgDays = new Array();
		var dataAvgFee = new Array();
		var dataDrugRatio = new Array();
		var dataAvgDaysCC = new Array();
		var dataAvgFeeCC = new Array();
		var dataDrugRatioCC = new Array();
		for (var i = 0; i < charts.length; i++) {
			category.push(charts[i].ddesc);
			dataAvgDays.push(charts[i].AvgDays);
			dataAvgFee.push(charts[i].AvgFee);
			dataDrugRatio.push(charts[i].DrugRatio);
			dataAvgDaysCC.push(charts[i].AvgDayscc);
			dataAvgFeeCC.push(charts[i].AvgFeecc);
			dataDrugRatioCC.push(charts[i].DrugRatiocc);
		}
		drawChart($('#'+tabId+' #depAvgDays-chart'),'平均住院日',category,dataAvgDays,dataAvgDaysCC);
		drawChart($('#'+tabId+' #depAvgFee-chart'),'次均费用',category,dataAvgFee,dataAvgFeeCC);
		drawChart($('#'+tabId+' #depDrugRatio-chart'),'药占比（%）',category,dataDrugRatio,dataDrugRatioCC);
	}

	function getClinicTable(data){
			var id1='id1'+new Date().getTime();
			$('#'+tabId+' #clinicMC-table').html('<table id="'+id1+'" class="stripe cc table" style="width:100%;">'
					+'<thead><tr>'
					+'<td>主诊组</td>'
					+'<td>CMI</td>'
					+'<td>DRGs组数</td>'
					+'<td>总权重</td>'
					+'<td>时间消耗<br>指数</td>'
					+'<td>费用消耗<br>指数</td>'
					+'<td>低风险<br>死亡人数</td>'
					+'</tr></thead><tbody></tbody></table>');
			var columns=[ 
	                     { "data": 'ddesc'},
	                     { "data": "CMI"},
	                     { "data": "DRGCount"},
  	                     { "data": "TotalWeight"},
	                     { "data": "TimeEff"},
	                     { "data": "FeeEff"},
  	                     { "data": "Risk1DeadCaseCnt"}
	                 ];
			var paramsDataTables={lengthChange:false,rownumbers:true,searching:false,info:false,ordering:false,scrollX:true,order:[[ 1, "desc" ]],columnDefs: [{ "orderable": false, "targets": [ 0 ] }]};
			var drawCallback=function( oSettings  ){
	   	    	 var table = $(this);
		    	 $(this).find("tbody").on( 'click', 'tr', function () {
		    		 	var thisTr = $(this).parent().parent();
						thisTr.toggleClass('selected');
						var bySelected = thisTr.parent().find("tr");
		    	        $(this).toggleClass('selected');
		    	        
		    	        var category = new Array();
		    		 	var series = new Array();
		    		 	table.find("tbody tr.selected").each(function(){
		    		 		category.push($(this).find("td:eq(0)").html());
		    		 		var serie = {name: $(this).find("td:eq(0)").html(), value:[Number($(this).find("td:eq(1)").html()), Number($(this).find("td:eq(6)").html()), Number($(this).find("td:eq(5)").html()), Number($(this).find("td:eq(4)").html()), Number($(this).find("td:eq(3)").html()), Number($(this).find("td:eq(2)").html())]}
		    		 		series.push(serie);
		    		 	})
	   	    	    	drawWebChart($('#'+tabId+' #clinicMC-chart'),'',category,series);
		    	    } );
		    	 if($(this).find("tbody").find("tr").size()>0){
		    		$(this).find("tbody").find("tr:eq(0)").click(); 
		    	 }
		    	 if($(this).find("tbody").find("tr").size()>1){
		    		setTimeout(function(){
		    		 table.find("tbody").find("tr:eq(1)").click();
		    	 },100) 
		    	 }
		    }
			paramsDataTables.drawCallback=drawCallback;
			var table = $('#'+tabId+' #'+id1).dataTableBasic(columns,data,paramsDataTables);

	}

	//主诊组未达目标值汇总表格
	function buildClinicQuotaTargetTable(selector,data){
		
		if (data!=undefined) {
			var id1='id1'+new Date().getTime();
			$(selector).html('<table id="'+id1+'" class="stripe cc table" style="width:100%;">'
					+'<thead><tr>'
					+'<td>主诊组</td>'
					+'<td>平均住院日</td>'
					+'<td>次均费用</td>'
					+'<td>药占比</td>'
					+'</tr></thead><tbody></tbody></table>');
			var columns=[ 
	                     { "data": 'ddesc'},
	                     { "data": "AvgDays" ,"type":"numeric-comma",
  	                       "render": function(obj, type, full) {
  	                    	 var AvgDays=obj.aData.AvgDays;
  	                           var AvgDays_EX=obj.aData.AvgDayscc;
  	                           return compareValue(AvgDays,AvgDays_EX);
  	                        
  	                         }
	                     },
	                     { "data": "AvgFee" ,"type":"numeric-comma",
	                       "render": function(obj, type, full) {
	                    	   var AvgFee=obj.aData.AvgFee;
  	                           var AvgFee_EX=obj.aData.AvgFeecc;
  	                           return compareValue(AvgFee,AvgFee_EX);
  	                         
  	                         }
	                     },
  	                     { "data": "DrugRatio" ,"type":"numeric-comma",
  	                       "render": function(obj, type, full) {
  	                    	 var DrugRatio=obj.aData.DrugRatio;
  	                           var DrugRatio_EX=obj.aData.DrugRatiocc;
  	                           return compareValue(DrugRatio,DrugRatio_EX);
  	                         
  	                         }
  	                     }
	                 ];
			var paramsDataTables={lengthChange:false,rownumbers:true,searching:false,info:false,ordering:false,order:[[ 1, "desc" ]],columnDefs: [{ "orderable": false, "targets": [ 3 ] }]};
			
			$('#'+tabId+' #'+id1).dataTableBasic(columns,data,paramsDataTables);
		}
	}
	
	
	function drawChart(container,title,category,data,dataCC){
		var legend=['本期值','目标值'];
		var xAxis =[{type : 'category',data : category}];
		var yAxis=[{ type : 'value'}];
		var series = [
				        {
				            name:'本期值',
				            type:'bar',
				            data:data
				        },
				        {
				            name:'目标值',
				            type:'line',
				            data:dataCC
				        }
				    ];
		var chartParams={isDataZoom:true,legendPosition:'bottom'};
		barLineMixChartBasic(container,legend,series,xAxis,yAxis,title, '',chartParams);
	}
	
	function drawWebChart(container,title,category,data){
		var quotaCode=['CMI','Risk1Ratio','FeeEff','TimeEff','TotalWeight','DRGCount'];
		var quotaDesc =['CMI','低风险死亡人数','费用消耗指数','时间消耗指数','总权重','DRGs组数'];
		//调公共方法组装数据
		var lines = [];
		var quotas=[];
		var echartData;
		for(var o in category){
			lines.push({desc:category[o],data:[]});
		}
		for(var o in data){
			var obj=[];
			for(var j in data[o].value){
				var temp;
				temp[quotaCode[j]]=data[o].value[j];
				obj.push(temp);
				if(o==0){//quotas只存一次，并max赋初始最大值
					quotas.push({code:quotaCode[j],text:quotaDesc[j],max:data[o].value[j]});
				}
				else{//如果o不等于0时，则判断当前quotas的max是否为最大值
					if(data[o].value[j]>quotas[j].max){
						quotas[j].max=data[o].value[j];
					}
				}
				
			}
			echartData.push(obj);
		}
		radarCharts(container, lines, echartData, quotas, '全院指标值比较图', '');
	}
})



